package com.zt.questionnaire.common.utils;

import java.util.regex.Pattern;

public class RegExpUtils {
    private static Pattern pwdRegex = Pattern.compile("^[0-9a-zA-Z]{8,16}$");
    private static Pattern phoneRegex = Pattern.compile("^((13[0-9])|(15[0-9])|(18[0-9])|(17[0-9])|(14[0-9])|(19[89]))\\d{8}$");
    private static Pattern strRegex = Pattern.compile("^[a-zA-Z0-9\\u4e00-\\u9fa5]+$");

    private static Pattern lanIpRegex = Pattern.compile("^((192\\.168|172\\.([1][6-9]|[2]\\d|3[01]))(\\.([2][0-4]\\d|[2][5][0-5]|[01]?\\d?\\d)){2}|10(\\.([2][0-4]\\d|[2][5][0-5]|[01]?\\d?\\d)){3})$");

    //15位身份证
    private static Pattern IDCard1 = Pattern.compile("/^[1-9]\\d{7}((0\\d)|(1[0-2]))(([0|1|2]\\d)|3[0-1])\\d{3}$/");

    //18位身份证
    private static Pattern IDCard2 = Pattern.compile("/^[1-9]\\d{5}[1-9]\\d{3}((0\\d)|(1[0-2]))(([0|1|2]\\d)|3[0-1])\\d{3}([0-9]|X)$/");

    //[\u4e00-\u9fa5，。！、……a-zA-Z\d]+
    private static Pattern pwdPayRegex = Pattern.compile("^[0-9]{6}$");

    public static void main(String[] args) {
        System.out.println(isPhone("15012062058"));
        System.out.println(isPhone("19116021191"));
    }

    /**
     * 是否为手机
     *
     * @param phone
     * @return
     */
    public static boolean isPhone(String phone) {
        return PatternUtils.isPhone(phone);
//		return phoneRegex.matcher(phone).matches();
    }

    /**
     * 是否为联系人
     *
     * @param contacts
     * @return
     */
    public static boolean isContacts(String contacts) {
        return strRegex.matcher(contacts).matches();
    }

    /**
     * 是否为公司名称
     *
     * @param company
     * @return
     */
    public static boolean isCompany(String company) {
        return strRegex.matcher(company).matches();
    }

    /**
     * 是否为密码
     *
     * @param pwd
     * @return
     */
    public static boolean isPwd(String pwd) {
        return pwdRegex.matcher(pwd).matches();
    }

    /**
     * 是否为支付密码
     *
     * @param pwd
     * @return
     */
    public static boolean isPayPwd(String pwd) {
        return pwdPayRegex.matcher(pwd).matches();
    }

    public static boolean isLanIp(String ip) {
        if ("127.0.0.1".equals(ip)) {
            return true;
        }
        return lanIpRegex.matcher(ip).matches();
    }

    /**
     * 是否为身份证
     *
     * @param idCard
     * @return
     */
    public static boolean isIdCard(String idCard) {
        if (idCard.length() == 15) {
            return IDCard1.matcher(idCard).matches();
        }
        if (idCard.length() == 18) {
            return IDCard2.matcher(idCard).matches();
        }
        return false;
    }

}
